<?php

namespace App\Filament\Resources\MemberResource\RelationManagers;

use App\Enums\BorrowStatus;
use App\Models\BorrowRecord;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class BorrowRecordRelationManager extends RelationManager
{
    protected static ?string $label = '借阅记录';
    protected static string $relationship = 'borrowRecord';

    protected static ?string $title = '借阅记录';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->required()
                    ->maxLength(255),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('book.title')
            ->columns([
                Tables\Columns\TextColumn::make('book.title')
                ->label('书名')
                ->searchable(),
                Tables\Columns\TextColumn::make('returned')
                    ->label('状态')
                    ->formatStateUsing(fn (BorrowRecord $record) =>
                            $record->returned->getLabelText()
                    )
                    ->badge()
                    ->color(fn (BorrowStatus $state): string => $state->getColor())
                    ->searchable(),
                Tables\Columns\TextColumn::make('borrow_date')
                    ->label('借阅时间')
                    ->searchable(),
                Tables\Columns\TextColumn::make('due_date')
                    ->label('归还时间')
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                //Tables\Actions\CreateAction::make(),
            ])
            ->actions([
//                Tables\Actions\EditAction::make(),
//                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}
